Recursive Descent Parsing
Table of Contents
A top-down and left-to-right parsing algorithm that terminals are seens in order of appearance in the token stream.
1. Recursive Descent Parsing Algorithm
Let TOKEN be the type of tokens, and we have a global next point to the next input token. And suppose we have several helper functions that returns true or false.
fn term(TOKEN tok)tells if the tokennextis pointing to matches a given token terminaltok, and advances thenextpointer.fn S_n()tries to match with n-th productionfn S()tries all productions
For example,
- for production
E -> T, the function can be defined asfn E_1() { return T(); }; - for production
E -> T+E, it can befn E_2() { return T() && term(+) && E(); }; - for all productions, we need backtracking (store current
nextpointer)
bool E() {
TOKEN* save = next;
return (next = save, E_1())
|| (next = save, E_2())
|| (...);
}
// And similarly for T()
bool T() {
TOKEN* save = next;
return (next = save, T_1())
|| (next = save, T_2())
|| (...);
}
To start the parser, we simply initialize next to point to the first token, and invoke E().
1.1. Limitation
RDP might cause a false reject of a correct pattern, if a prefix of a pattern is also a valid pattern. i.e., we cannot backtrack to try a different production for \(X\) later, if a production for non-terminal \(X\) succeeds.